package com.ln.cmsln.controller.bai;

import com.alibaba.fastjson.JSON;
import com.ln.cmsln.pojo.entity.bai.Permission;
import com.ln.cmsln.pojo.entity.bai.Role;
import com.ln.cmsln.service.bai.RoleService;
import com.ln.cmsln.service.bai.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author baiyuxiang
 * @description 角色控制
 * @date 2022/3/31 20:38
 * @Version 1.0
 **/
@RestController
@RequestMapping("/rc")
@CrossOrigin
public class RoleController {
    @Resource
    private RoleService roleService;

    @Resource
    private UserService userService;


    //角色信息全查
    @RequestMapping("/findall")
    public Object findall(){
        List<Role> list = roleService.findAllinfo();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("count", 0);
        map.put("data", list);
        return map;
    }

    //角色单个删除
    @RequestMapping("/delRole")
    @ResponseBody
    public String delUser(Role role){

        try {
            roleService.delUser(role);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "no";
        }
    }

    //角色批量删除
    @RequestMapping("/deluserss")
    @ResponseBody
    public String delss(String  id){
        try {
            String[] split = id.split(",");
            List<String> list = Arrays.asList(split);
            roleService.delss(list);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "no";
        }

    }

    //角色添加
    @RequestMapping("/addUser")
    @ResponseBody
    public String addUser(Role role){

        try {
            roleService.addUser(role);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "no";
        }

    }

    //角色去修改界面
    @RequestMapping("/up1")
    public String toUpdate(Role role,Model model){
        try {
            //根据id查询xuesheng信息
            Role stu = roleService.toUpdate(role);
            model.addAttribute("user",stu);
            return "role/edit";
        } catch (Exception e) {
            e.printStackTrace();
            return "no";
        }

    }

    //角色修改
    @RequestMapping("/up")
    @ResponseBody
    public String updateInfo(Role role){
        try {
            roleService.updateInfo(role);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "no";
        }

    }

    //角色赋权限
    @RequestMapping("/findAllss")
    @ResponseBody
    public Object findAllss(String id){
        //权限的全查
        List<Permission> list = userService.finAllPre();
        //根据id查询已有权限
        List<String> list2 = roleService.findbyrid(id);
        for (Permission permission : list) {
            for (String string : list2) {
                if(permission.getId().contains(string)){
                    permission.setChecked(true);
                }
            }
        }
        return list;
    }

    @RequestMapping("/addperrole")
    @ResponseBody
    public String addperrole(String w, String id){
        try {

            //先删除
            //List<RolePermission> list2 = roleService.getFindbyrid(id);

            roleService.delperrole(id);
            //后添加
            String[] split = w.split(",");
            List<String> list = Arrays.asList(split);

            roleService.addperrole(id,list);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "no";
        }


    }
}
